<template>
	<view>
		<!-- 搜索栏 -->
		<view class="search">
			<my-search></my-search>
		</view>
		<view class="scroll-view-container">
			<scroll-view scroll-y class="scroll-left" :style="{height:wh+'px'}">
				<view :class="['scroll-left-item',active==index?'active':'']" v-for="(item,index) in cateList"
					:key='index' @click="activeChange(index)">{{item.cat_name}}</view>
			</scroll-view>
			<scroll-view scroll-y class="scroll-right" :style="{height:wh+'px'}" :scroll-top="scrollTop">
				<view class="cate-lv2-list" v-for="(item,index) in cateLevel2" :key='index'>
					<view class="cate-lv2-title">/{{item.cat_name}}/</view>
					<view class="cate-lv3-list">
						<navigator :url="`/subpkg/goods_list/goods_list?cat_id=${e.cat_id}`" class="cate-lv3-item" v-for="(e,i) in item.children" :key='i'>
							<image :src="e.cat_icon" mode=""></image>
							<text>{{e.cat_name}}</text>
						</navigator>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
import TabBarBadge from '../../mixins/tabbar-badge.js'
	export default {
		mixins:[TabBarBadge],
		data() {
			return {
				active: 0,
				wh: 0, //屏幕高度
				cateList: [], //分类列表数据
				cateLevel2: [], //二级分类数据
				scrollTop:0//scroll与顶部的距离
			};
		},
		onLoad() {
			this.wh = uni.getSystemInfoSync().windowHeight-50
			this.getCateList()
		},
		methods: {
			// 获取分类列表数据
			async getCateList() {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/categories')
				// console.log(res);
				if (meta.status !== 200) {
					return uni.$showMsg()
				}
				this.cateList = message
				// 二级分类数据
				this.cateLevel2 = message[0].children
			},
			activeChange(index) {
				this.active = index
				// 按照下标重新获取二级数据
				this.cateLevel2 = this.cateList[index].children
				this.scrollTop=this.scrollTop==0?0:0.1
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;

		.scroll-left {
			width: 120px;

			.scroll-left-item {
				font-size: 12px;
				text-align: center;
				background-color: #f7f7f7;
				line-height: 60px;

				&.active {
					background-color: #fff;
					position: relative;

					&::before {
						content: '';
						width: 3px;
						height: 30px;
						background-color: #c00000;
						left: 0;
						top: 50%;
						margin-top: -15px;
						position: absolute;
					}
				}
			}
		}

		.scroll-right {
			.cate-lv2-title {
				font-size: 12px;
				font-weight: 700;
				text-align: center;
				padding: 15px 0;
			}

			.cate-lv3-list {
				display: flex;
				flex-wrap: wrap;

				.cate-lv3-item {
					width: 33.33%;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
					margin-bottom: 10px;
					image{
						width: 60px;
						height: 60px;
					}
					text{
						font-size: 12px;
					}
				}
			}
		}
	}
</style>
